﻿using System;
using System.IO;
using System.Xml.Serialization;
using System.Windows.Forms;

namespace KiraSh.ResumeTracker
{
    // helper class to work with xml files
    public class XMLDealer
    {
        // load xml file and return the result object
        public object LoadXML(string FileName, Type VarType)
        {
            try
            {
                Stream stream = new FileStream(FileName, FileMode.Open);
                XmlSerializer serializer = new XmlSerializer(VarType);
                object o = serializer.Deserialize(stream);
                stream.Close();
                return o;
            }
            // if the file doesn't exist return null
            catch
            {
                return null;
            }
        }

        // write object to the xml file
        public void WriteXML(string FileName, Type VarType, object o)
        {
            try
            {
                Stream stream = new FileStream(FileName, FileMode.Create);
                XmlSerializer serializer = new XmlSerializer(VarType);
                serializer.Serialize(stream, o);
                stream.Close();
            }
            // smth went wrong display message
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }
    }
}
